<?php

require_once("database.inc.php");
require_once("functions.inc.php");

ini_set("memory_limit","512M");

$leagueids = array();
$lastupdate = getLastupdate();

$dbh = connect_db("dbm");

$teams = mysql_query("SELECT teamid, leagueid FROM `teams_$lastupdate` ORDER BY teamid", $dbh);

while ($team = mysql_fetch_array($teams)) {
	$leagueids[$team['teamid']] = $team['leagueid'];
}

$lastleagueid = 0;
$lastteamid = 0;

foreach (array_keys($leagueids) as $teamid) {
	if ($leagueids[$teamid] != $lastleagueid) {
		if ($lastleagueid != 0) { echo "$lastteamid) { return $lastleagueid; }\n"; }
		echo "if (\$teamid >= $teamid && \$teamid <= ";
	}
	$lastleagueid = $leagueids[$teamid];
	$lastteamid = $teamid;
}
echo "$lastteamid) { return $lastleagueid; }\n";
?>
